#include "bsp.h"

#define DAC_OUT_VAL0         0x7FF0
#define DAC_OUT_VAL1         0x1FF0

static void rcu_config(void);
static void gpio_config(void);

void DAC_Init()
{
		rcu_config();
    gpio_config();
    dac_config(0);
}

static void rcu_config(void)
{
    /* enable the clock of peripherals */
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_DAC);
}

static void gpio_config(void)
{
    /* once enabled the DAC, the corresponding GPIO pin is connected to the DAC converter automatically */
    gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
}

void dac_config(uint16_t _dac_out_val1)
{
    dac_deinit();
//    /* configure the DAC_OUT0 */
//    dac_trigger_disable(DAC_OUT_0);
//    dac_wave_mode_config(DAC_OUT_0, DAC_WAVE_DISABLE);
//    dac_output_buffer_enable(DAC_OUT_0);

    /* configure the DAC_OUT1 */
    dac_trigger_disable(DAC_OUT_1);
    dac_wave_mode_config(DAC_OUT_1, DAC_WAVE_DISABLE);
    dac_output_buffer_enable(DAC_OUT_1);

    /* enable DAC concurrent mode and set data */
    dac_concurrent_enable();
    dac_concurrent_data_set(DAC_ALIGN_12B_R, _dac_out_val1, _dac_out_val1);
}